﻿using CMS.Utils;
using Microsoft.Data.Sqlite;
using System.Data;

namespace CMS.DataAccess
{
    public class ReportLoader
    {
        DbInstance instance;
        public ReportLoader()
        {
            instance = ServiceLoader.GetService<DbInstance>();
        }
        public DataTable LoadSql(string sql,params SqliteParameter[] where)
        {
            DataTable dataTable = new DataTable();
            using (var cmd = instance.MakeCommand(sql, where))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        string columnName = reader.GetName(i);
                        dataTable.Columns.Add(columnName);
                    }
                    while (reader.Read())
                    {
                        var row = dataTable.Rows.Add();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            row[i] = reader[i];
                        }
                    }
                }
            }
            return dataTable;
        }
    }
}
